﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReBasic.Helper
{
    public class ProBaseLoader
    {
        public delegate void ShowInfoHandler(string str);
        public delegate void SimpleInfoHandler(int r_id);
        public event SimpleInfoHandler AddConceptEvent;
        public event SimpleInfoHandler ChangeEvent;
        public event SimpleInfoHandler SkipEvent;
        public event ShowInfoHandler ShowLastStrEvent;

        ProBaseHeper pbh;
        StreamReader sr, srdb;
        StreamWriter sw;
        FileStream fsr, fsw, fsrdb;

        string path = @"F:\Relationship Extraction\"
            + "core47_5.tax";
        string tabPath = @"F:\Relationship Extraction\"
            + "core_change_tab.tax";
        string linePath = @"F:\Relationship Extraction\"
            + "core_change_line.tax";
        string sentPath = @"F:\Relationship Extraction\data\"
            + "sentences_tw_lc_wp_ba_ns.txt";

        void OnAddConceptEvent(int r_id)
        {
            if (AddConceptEvent != null)
                AddConceptEvent(r_id);
        }

        void OnChangeEvent(int c_id)
        {
            if (ChangeEvent != null)
                ChangeEvent(c_id);
        }

        void OnSkipEvent(int s_id)
        {
            if (SkipEvent != null)
                SkipEvent(s_id);
        }

        void OnShowLastStrEvent(string str)
        {
            if (ShowLastStrEvent != null)
                ShowLastStrEvent(str);
        }

        public void DbSent()
        {
            const int start = 0;
            int cnt = start, skip = 0;
            fsr = new FileStream(sentPath, FileMode.Open);
            sr = new StreamReader(fsr, Encoding.UTF8);
            pbh = new ProBaseHeper();
            pbh.DataBind();
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                str = str.Replace("'", "''");
                if (str.Length > 490) continue;
                if (skip >= start)
                {
                    if (skip == start)
                    {
                        OnShowLastStrEvent(str);
                        skip++;
                    }
                    pbh.AddSent(str);
                    ++cnt;
                    if (cnt % 1000 == 0)
                        OnChangeEvent(cnt);
                }
                else
                {
                    ++skip;
                    if (skip % 10000 == 0)
                        OnSkipEvent(skip);

                }
            }
            OnChangeEvent(cnt);
            pbh.Terminate();
            sr.Close();
            fsr.Close();
        }

        public void DbChange()
        {
            int cnt = 0;
            fsr = new FileStream(path, FileMode.Open);
            sr = new StreamReader(fsr, Encoding.UTF8);
            fsw = new FileStream(tabPath, FileMode.Create);
            sw = new StreamWriter(fsw, Encoding.UTF8);
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                string[] spt = str.Split('\t');
                sw.WriteLine(spt[0].Replace("'", "''"));
                sw.WriteLine(spt[1].Replace("'", "''"));
                sw.WriteLine(spt[2]);
                ++cnt;
                if (cnt % 10000 == 0)
                    OnChangeEvent(++cnt);
            }
            sr.Close();
            fsr.Close();
            sw.Close();
            fsw.Close();
        }

        public void LoadAllConcepts_Line()
        {
            int cnt = 0;
            string s0, s1, s2;
            fsrdb = new FileStream(linePath, FileMode.Open);
            srdb = new StreamReader(fsrdb, Encoding.UTF8);
            pbh = new ProBaseHeper();
            pbh.DataBind();
            while (!srdb.EndOfStream)
            {
                s0 = srdb.ReadLine();
                s1 = srdb.ReadLine();
                s2 = srdb.ReadLine();
                pbh.AddConcept(s0, s1, s2);
                ++cnt;
                if (cnt % 10000 == 0)
                    OnAddConceptEvent(cnt);
            }
            pbh.Terminate();
            srdb.Close();
            fsrdb.Close();
        }


        public void LoadAllConcepts_Tab()
        {
            int cnt = 0;
            string str;
            fsrdb = new FileStream(tabPath, FileMode.Open);
            srdb = new StreamReader(fsrdb, Encoding.UTF8);
            pbh = new ProBaseHeper();
            pbh.DataBind();
            while (!srdb.EndOfStream)
            {
                str = srdb.ReadLine();
                string[] spt = str.Split('\t');
                pbh.AddConcept(spt[0], spt[1], spt[2]);
                ++cnt;
                if (cnt % 10000 == 0)
                    OnAddConceptEvent(cnt);
            }
            pbh.Terminate();
            srdb.Close();
            fsrdb.Close();
        }

        public void CloseChangeFile()
        {
            try
            {
                sr.Close();
                fsr.Close();
                sw.Close();
                fsw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void CloseFile()
        {
            try
            {
                srdb.Close();
                fsrdb.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
